package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSkuVo;
import com.spzx.user.domain.UserBrowseHistory;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.mapper.UserCollectMapper;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Autowired
    private UserCollectMapper userCollectMapper;
    @Autowired
    private RemoteProductService remoteProductService;
    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;


    @Override
    public void register(UserInfo userInfo) {
        long count = baseMapper.selectCount(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, userInfo.getUsername())
        );
        if (count > 0) {
            throw new ServiceException("手机号已经存在");
        }
        baseMapper.insert(userInfo);
    }

    @Override
    public UserInfo selectUserByUserName(String username) {
        return baseMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, username)
        );
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        baseMapper.updateById(userInfo);
        return true;
    }

    @Override
    public List<ProductSkuVo> getUserCollectList() {
        List<UserCollect> userCollectList = userCollectMapper.selectList(null);
        if (!CollectionUtils.isEmpty(userCollectList)) {
            List<ProductSkuVo> productSkuVoList = userCollectList.stream().map(userCollect -> {
                Long skuId = userCollect.getSkuId();
//            LambdaQueryWrapper<ProductSku> skuLambdaQueryWrapper = new LambdaQueryWrapper<ProductSku>()
//                    .eq(ProductSku::getId, skuId);
                R<ProductSkuVo> productSkuVoResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
                if (R.FAIL == productSkuVoResult.getCode()) {
                    throw new ServiceException(productSkuVoResult.getMsg());
                }
                ProductSkuVo productSkuVo = productSkuVoResult.getData();
                productSkuVo.setSkuId(skuId);
//            R<ProductSku> productSkuTwoResult = remoteProductService.getProductSkuTwo(skuId, SecurityConstants.INNER);
//            if (R.FAIL == productSkuTwoResult.getCode()) {
//              throw new ServiceException(productSkuTwoResult.getMsg());
//          }
//                ProductSku productSku = productSkuTwoResult.getData();
//                userCollect.setProductSku(productSku);
                return productSkuVo;
            }).collect(Collectors.toList());
            return productSkuVoList;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<ProductSkuVo> getUserBrowseHistoryList() {
        List<UserBrowseHistory> userBrowseHistoryList = userBrowseHistoryMapper.selectList(null);
        if (!CollectionUtils.isEmpty(userBrowseHistoryList)) {
            List<ProductSkuVo> productSkuVoList = userBrowseHistoryList.stream().map(userBrowseHistory -> {
                Long skuId = userBrowseHistory.getSkuId();
                R<ProductSkuVo> productSkuVoResult = remoteProductService.getProductSku(skuId, SecurityConstants.INNER);
                if (R.FAIL == productSkuVoResult.getCode()) {
                    throw new ServiceException(productSkuVoResult.getMsg());
                }
                ProductSkuVo productSkuVo = productSkuVoResult.getData();
                productSkuVo.setSkuId(skuId);
                return productSkuVo;
            }).collect(Collectors.toList());
            return productSkuVoList;
        }
        return new ArrayList<>();
    }
}
